package erabbit.net.camerabot;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Bundle;
import android.view.View;

import com.baidu.idl.face.platform.FaceSDKManager;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends TakePhotoActivity {
    ArrayList<TImage> images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        requestPermissions(99, Manifest.permission.CAMERA);

        initLib();

    }



    /**
     * 初始化SDK
     */
    private void initLib() {
        // 第一个参数 应用上下文
        // 第二个参数 APPID=appname-face-android ,其中appname为申请SDK时填写的应用名称
        // 第三个参数 assets目录下的License文件名
        //FaceSDKManager.getInstance().initialize(this, "自动照相机器人-face-android", "idl-license.face-android");
        FaceSDKManager.getInstance().initialize(this, "CameraBot-face-android", "idl-license.face-android");
        // setFaceConfig();
    }


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        showImg(result.getImages());
    }

    private void showImg(ArrayList<TImage> images) {
        Intent intent = new Intent(this, ShowImageActivity.class);
        intent.putExtra("images", images);
        startActivity(intent);
    }


    public void getFace(View view){
       //Intent intent = new Intent(this, FaceDetectActivity.class);
       // Intent intent = new Intent(this, MiniActivity.class);
        Intent intent = new Intent(this, CamerBotActivity.class);
       // Intent intent = new Intent(this, FacePlusPlusActivity.class);
        startActivity(intent);
    }


    public void takePhoto(View view) {

       // File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/"+ "camerabot.jpg");
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        //configCompress(getTakePhoto());
        //configTakePhotoOption(getTakePhoto());
        getTakePhoto().onPickFromCapture(imageUri);
    }


    public void customTakePhoto(View view) {

//        Intent intent = new Intent(this, CustomCameraActivity.class);
//        startActivity(intent);

 //       Intent intent = new Intent(this, FacePlusPlusActivity.class);
        Intent intent = new Intent(this, MiniActivity.class);
        startActivity(intent);
    }



    public void requestPermissions(int requestCode, String permission) {
        if (permission != null && permission.length() > 0) {
            try {
                if (Build.VERSION.SDK_INT >= 23) {
                    // 检查是否有权限
                    int hasPer = checkSelfPermission(permission);
                    if (hasPer != PackageManager.PERMISSION_GRANTED) {
                        // 是否应该显示权限请求
                        boolean isShould = shouldShowRequestPermissionRationale(permission);
                        requestPermissions(new String[]{permission}, requestCode);
                    }
                } else {

                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        boolean flag = false;
        for (int i = 0; i < permissions.length; i++) {
            if (PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                flag = true;
            }
        }
        if (!flag) {
            requestPermissions(99, Manifest.permission.CAMERA);
        }
    }


}
